/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package services.Impl;

import java.util.Hashtable;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import models.Users;
import services.interfaces.IRegService;
import services.interfaces.IUserService;

/**
 *
 * @author amega
 */
@Stateless
@LocalBean
public class RegServiceImpl extends GenericServiceImpl<Users> implements IRegService {

    @PersistenceContext(unitName="orsPU")
    private EntityManager em;
    
    public RegServiceImpl() {
        super(Users.class);
    }
    
    @Override
    protected EntityManager getEntityManager() {
        return  em;
    }

    @Override
    public boolean checkUser(Users user) {
        String username = "amega";
        String password = "qwerty";
        String base = "ou=people,dc=example,dc=com";
        String dn = "uid=" + username + "," + base;
        String ldapURL = "ldap://ldap.example.com:389";

        Hashtable<String,String> env = new Hashtable<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, ldapURL);
        env.put(Context.SECURITY_AUTHENTICATION,"simple");
        env.put(Context.SECURITY_PRINCIPAL,dn);
        env.put(Context.SECURITY_CREDENTIALS, password);
        
        try {
            DirContext au = new InitialDirContext(env);
            return  true;
        } catch (AuthenticationException  e) {
            
        } catch (NamingException e) {
            //e.printStackTrace();
        }
        
        return false;
        
        
    }

    @Override
    public Long regUser(Users users) {
        em.persist(users);
        return users.getId();
    }
    
}
